// js 精度计算

function isInteger(obj) {
    return Math.floor(obj) === obj
}

function toInteger(floatNum) {
    var ret = {times: 1, num: 0}
    var isNegative = floatNum < 0
    if (isInteger(floatNum)) {
        ret.num = floatNum
        return ret
    }
    var strfi = floatNum + ''
    var dotPos = strfi.indexOf('.')
    var len = strfi.substr(dotPos + 1).length
    var times = Math.pow(10, len)
    var intNum = parseInt(Math.abs(floatNum) * times + 0.5, 10)
    ret.times = times
    if (isNegative) {
        intNum = -intNum
    }
    ret.num = intNum
    return ret
}

function operation(a, b, digits, op) {
    var o1 = toInteger(a);
    var o2 = toInteger(b);
    var n1 = o1.num;
    var n2 = o2.num;
    var t1 = o1.times;
    var t2 = o2.times;
    var max = t1 > t2 ? t1 : t2;
    var result = null;
    switch (op) {
        case 'add':
            if (t1 === t2) { // 两个小数位数相同
                result = n1 + n2
            } else if (t1 > t2) { // o1 小数位 大于 o2
                result = n1 + n2 * (t1 / t2)
            } else { // o1 小数位 小于 o2
                result = n1 * (t2 / t1) + n2
            }
            if (digits) {
                return toFixedDigits(result / max, digits);
            }
            return result / max;
        case 'subtract':
            if (t1 === t2) {
                result = n1 - n2
            } else if (t1 > t2) {
                result = n1 - n2 * (t1 / t2)
            } else {
                result = n1 * (t2 / t1) - n2
            }
            if (digits) {
                return toFixedDigits(result / max, digits)
            }
            return result / max;
        case 'multiply':
            result = (n1 * n2) / (t1 * t2);
            if (digits) {
                return toFixedDigits(result, digits)
            }
            return result;
        case 'divide':
            result = (n1 / n2) * (t2 / t1);
            if (digits) {
                return toFixedDigits(result, digits);
            }
            return result;
    }
}

function toFixedDigits(num, digits) {
    let times = Math.pow(10, digits);
    var o1 = toInteger(times);
    var o2 = toInteger(num);
    var n1 = o1.num;
    var n2 = o2.num;
    var t1 = o1.times;
    var t2 = o2.times;
    var result = null;
    result = (n1 * n2) / (t1 * t2);
    return parseInt(result, 10) / times;
}

// 加减乘除的四个接口
//a b 待处理数据  digits为0表示不处理小数精度位 1表示保留一位小数 以此类推
export function add(a, b, digits) {
    return operation(a, b, digits, 'add')
}

export function subtract(a, b, digits) {
    return operation(a, b, digits, 'subtract')
}

export function multiply(a, b, digits) {
    return operation(a, b, digits, 'multiply')
}

export function divide(a, b, digits) {
    return operation(a, b, digits, 'divide')
}
